import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/common/DemoLocalizations.dart';
import 'package:flutter_app/common/ProfileChangeNotifier.dart';
import 'package:provider/provider.dart';

class MyDrawer extends StatelessWidget {

  const MyDrawer({Key key,}) :super(key: key);

  @override
  Widget build(BuildContext context) {
    return Drawer(
      //移除顶部padding
        child: MediaQuery.removePadding(
            context: context,
            removeTop: true,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                _buildHeader(), //构建抽屉菜单头部
                Expanded(child: _buildMeuns()) //构建功能菜单
              ],
            ))
    );
  }
}

Widget _buildHeader() {
  return Consumer<UserModel>(
    builder: (BuildContext context, UserModel value, Widget child) {
      return GestureDetector(
          child: Container(
            color: Theme
                .of(context)
                .primaryColor,
            padding: EdgeInsets.only(top: 40, bottom: 20),
            child: Row(
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 16.0),
                  child: ClipOval(
                    // 如果已登录，则显示用户头像；若未登录，则显示默认头像
                    child: value.isLogin ?
                    loadAvatar(value.user.avatar_url, width: 80) :
                    Image.asset("imgs/avatar-default.png", width: 80,)
                    ,
                  ),
                ),
                Text(
                  value.isLogin ? value.user.login : DemoLocalizations
                      .of(context)
                      .title,
                  style: TextStyle(
                      fontWeight: FontWeight.bold,
                      color: Colors.white
                  ),
                ),
              ],
            ),
          ),
          onTap: () {
            if (!value.isLogin) {
              Navigator.of(context).pushNamed("login");
            }
          }
      );
    },
  );
}

// 构建菜单项
Widget _buildMeuns() {
  return Consumer<UserModel>(
      builder: (BuildContext context, UserModel value, Widget child) {
        var gm = DemoLocalizations.of(context);
        return ListView(
          children: <Widget>[
            ListTile(
              leading: const Icon(Icons.color_lens),
              title: Text(gm.title),
              onTap: () => Navigator.of(context).pushNamed("theme"),
            ),
            ListTile(
              leading: const Icon(Icons.language),
              title: Text(gm.title),
              onTap: () => Navigator.of(context).pushNamed("language"),
            ),
            if(value.isLogin)
              ListTile(
                leading: const Icon(Icons.power_settings_new),
                title: Text(gm.title),
                onTap: () {
                  showDialog(context: context,
                      builder: (context) {
                        //退出账号前先弹二次确认窗
                        return AlertDialog(
                          content: Text(gm.title),
                          actions: <Widget>[
                            FlatButton(
                              child: Text("cancle"),
                              onPressed: () => Navigator.pop(context),
                            ),
                            FlatButton(onPressed: () {
                              //该赋值语句会触发MaterialApp rebuild
                              value.user = null;
                              Navigator.pop(context);
                            }, child: Text("yes")),
                          ],
                        );
                      }
                  );
                },
              )
            ,
          ],
        );
      });
}

Widget loadAvatar(String avatar_url,
    {double width = 20,
      double height,
      BoxFit boxFit,
      BorderRadius borderRadius}) {
  var placeholder = Image.asset(
    "imgs/avatar-default.png", //头像占位图，加载过程中显示
    width: width,
    height: height,
  );
  return ClipRRect(
    borderRadius: borderRadius ?? BorderRadius.circular(2),
    child: CachedNetworkImage(
      imageUrl: avatar_url,
      width: width,
      height: height,
      fit: boxFit,
      placeholder: (context, url) => placeholder,
      errorWidget: (context, url, error) => placeholder,
    ),
  );
}
